#coding:utf-8

from app.lib import MysqlPool
import env_config
from datetime import datetime,timedelta
from app.models import DataDetail
from app.utils.util import str_random,str_timestamp

def member(user):
    user.extra['member_start_time'] = datetime.now().strftime("%Y-%m-%d")
    memberinfo = env_config.MEMBER_LIST.get(user.member_type)
    user.extra.update(memberinfo)
    user.extra['member_end_time'] = (datetime.now() + timedelta(int(user.extra.get('member_days')))).strftime("%Y-%m-%d")
    member_add_crm(user)
    member_add_rcs(user)
    return True

def member_add_crm(user):
    dbinfo = env_config.ENV_LIST.get(user.env.upper()).get('crf_crm')
    dbtool = MysqlPool.MysqlPool(dbinfo.get('db_name'))

    if user.member_system =='消费贷':
        sql = "INSERT INTO %(table_name)s.crf_member_info_%(db_prefix)s (crf_uid, member_type, member_name, procurement_type, amount, start_date, end_date, member_dues, member_sale_price, effective_days, can_refund, refund_amount, d_handle_fee_rate, m_handle_fee_rate, d_handle_fee_reduce, m_handle_fee_reduce, product_no, group_id, status, create_user, create_time, update_user, update_time) VALUES ('%(crfuid)s',%(member_type)s,'%(member_name)s',1,%(member_amount)s,'%(member_start_time)s','%(member_end_time)s', 0, 0, '', '0', 0, '', '', '', '', '%(member_product)s', '', 1, 'admin','%(timestamp)s','admin','%(timestamp)s');" \
              % {
                  'table_name': dbinfo.get('table_name'),
                  'db_prefix': user.extra.get('db_prefix'),
                  'crfuid': user.crfuid,
                  'member_type': user.extra.get('member_type'),
                  'member_name': user.extra.get('member_name'),
                  'member_amount': user.extra.get('member_amount'),
                  'member_start_time': user.extra.get('member_start_time'),
                  'member_end_time': user.extra.get('member_end_time'),
                  'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                  'member_product':user.extra.get('member_product')

              }
        print(sql)
        dbtool.insertOne(sql,None)

    elif user.member_system =='轻生活':
        sql ="INSERT INTO {}.crf_qshd_member_info ( crf_uid, order_no, member_type, member_name, procurement_type, start_date, trial_date, hesitate_date, end_date, member_dues, member_sale_price, amount, effective_days, can_refund, refund_amount, d_handle_fee_rate, m_handle_fee_rate, d_handle_fee_reduce, m_handle_fee_reduce, product_no, group_id, is_refund_fee, status, create_user, create_time, update_user, update_time) VALUES ('{}', '{}', {}, '{}', 1, '{}', '', '', '{}', {}, {}, {}, '150', 'N', 0, '', '', '', '', '{}', '', 0, 1, 'admin', '2018-04-03 15:31:33', 'admin', '2018-05-10 17:42:29');"\
            .format(dbinfo.get('table_name'),user.crfuid,str_timestamp(),user.extra.get('member_type'),user.extra.get('member_name'),user.extra.get('member_start_time'),user.extra.get('member_end_time'),user.extra.get('member_amount'),user.extra.get('member_amount'),user.extra.get('member_amount'),user.extra.get('member_product'))
        print(sql)
        dbtool.insertOne(sql, None)
    return True

def member_add_rcs(user):
    dbinfo = env_config.ENV_LIST.get(user.env.upper()).get('crf_rcs_agent_db')
    dbtool = MysqlPool.MysqlPool(dbinfo.get('db_name'))
    sql = "INSERT INTO {}.txn_purchase_order (org_no, crf_uid, mct_no, trxn_no, order_no, mct_order_no, order_date, order_handler_date, order_amt, order_type, level_type, order_trxn_type, order_status, coupon_amt, status_code, status_msg, other_status_code, other_status_msg, order_purpose, expire_date, user_name, id_no, user_phone, loan_flag, modify_date, insert_date, insert_user, version) VALUES ('CRF01', '{}', 'CRF0000001', {}, {}, '{}', '{} 02:12:46', '{} 02:12:46', {}, 1, {}, 1, 60, 0.00, '0', '!', null, null, '', '{} 02:12:46', null, null, null, 0, '{} 15:42:27', '{} 02:27:08', '', 0);" \
        .format(dbinfo.get('table_name'), user.crfuid, str_timestamp(), str_timestamp(), str_random(32),
                user.extra.get('member_start_time'), user.extra.get('member_start_time'),str(int(float(user.extra.get('member_amount')))//100), user.extra.get('member_type'),
                user.extra.get('member_end_time'), user.extra.get('member_start_time'),
                user.extra.get('member_start_time'))
    print(sql)
    dbtool.insertOne(sql, None)
    sql = "INSERT INTO {}.txn_purchase_order_detail ( org_no, crf_uid, mct_no, order_no, order_amt, member_level, member_name, member_dues, member_sale_price, member_effective_days, member_can_refund, member_refund_amount, member_dHandle_fee_discnt_rate, member_mHandle_fee_discnt_rate, member_dHandle_fee_reduce, member_mHandle_fee_reduce, member_probation_period, refund_hesitate_period, product_no, expire_date, modify_date, insert_date, version) VALUES ('CRF01', '{}', 'CRF0000001', {}, {}, '1', '{}', '{}', '{}', '365', 'N', '', '1', '1', '0', '0', '0', '0', 'P600000{}', '{} 18:21:20', '{} 18:21:20', '{} 18:21:20', 0);" \
        .format(dbinfo.get('table_name'), user.crfuid, str_timestamp(), str(int(float(user.extra.get('member_amount'))) // 100),user.extra.get('member_name'), str(int(float(user.extra.get('member_amount'))) // 100),
                str(int(float(user.extra.get('member_amount'))) // 100),user.extra.get('member_type'),
                user.extra.get('member_end_time'), user.extra.get('member_start_time'),
                user.extra.get('member_start_time'))
    print(sql)
    dbtool.insertOne(sql, None)
    return True



if __name__ =='__main__':
    user =DataDetail()
    user.extra['db_prefix']='1'
    user.crfuid='1211113'
    user.member_system = '轻生活'
    user.env='UAT'
    user.member_type='黄金年卡'
    member(user)